<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class Kickback extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'index_admin/login' );
			exit ();
		}
	}
	/*
	 * 返回商品列表
	 */
	public function index() {
		$id = $this->uri->segment(4);
		
		$this->load->library ( 'pagination' );
		// 每页显示数据条数
		$per_page=10;//暂定为10条
		
		$config['base_url']=site_url('kickback/index/id/'.$id);
		$this->load->model ( 'kickback_model' );
		$this->load->model ( 'user_model' );
		// 数据总数
		$config ['total_rows'] = $this->kickback_model->count_kickbacks($id);
		//$config ['total_rows']=200; 
		$config ['per_page'] = $per_page;
		$config ['first_link'] = '第一页';
		$config ['last_link'] = '最后一页';
		$config ['next_link'] = '下一页';
		$config ['prev_link'] = '上一页';
		$config['uri_segment'] =5;
		
		$this->pagination->initialize($config);
		$data['links']=$this->pagination->create_links();
		//偏移量
		$offset=intval($this->uri->segment(5));
		$data ['kickbacks'] = $this->kickback_model->find_kickbacks ($id,$per_page,$offset);
		$data ['userid'] =$id;
		$data ['user']=$this->user_model->load($id);
		$data ['title'] = '商品列表 - 回扣管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'kickback/index' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回添加商品页面
	 */
	public function add() {
		$id = $this->uri->segment(4);
		$data ['userid'] =$id;
		$data ['title'] = '添加商品 - 回扣管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'kickback/add' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回编辑商品页面
	 */
	public function edit() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'kickback_model' );
		$data ['kickback'] = $this->kickback_model->load ( $id );
		$data ['title'] = '编辑商品 - 回扣管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'kickback/edit' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 保存或者更新商品的信息
	 */
	public function save() {
		$id = $this->input->post('id');
		
		$name = $this->input->post('name');
		$content=explode('-',$name);
		$productid=$content[1];
		if(empty($productid)){
			return_json(FALSE,'','商品ID不能为空！');
		}
		
		$kickback = array (
				'productid' => $productid,
				'userid' => $this->input->post ( 'userid' ),
				'rate' => $this->input->post ( 'rate' ),
				'newprice' => $this->input->post ( 'newprice' )
		);
		$this->load->model ( 'kickback_model' );
		if ($id) {
			if($this->kickback_model->update ( $id, $kickback )){
				return_json(TRUE,'','修改成功！'); 
			}else{
				return_json(FALSE,'','信息未改动！');
			}
		} else {
			$kickbackid=$this->kickback_model->create ( $kickback );
			if(!empty($kickbackid)){
				return_json(TRUE,'','添加成功！'); 
			}else{
				return_json(FALSE,'','添加失败！');
			}
		}
	}
	/*
	 * 删除商品信息
	 */
	public function delete() {
		$id = $this->input->post('id');
		$this->load->model ( 'kickback_model' );
		if ($this->kickback_model->delete ( $id )) {
			return_json(TRUE,'','删除成功！'); 
		}else{
			return_json(TRUE,'','删除失败！');
		}
	}
	/*
	 * 返回每月下单金额
	 */
	public function order() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'user_model' );
		$data ['user']=$this->user_model->load ( $id );
		$this->load->model ( 'order_model' );
		$data ['orders'] = $this->order_model->get_month_total_by_userid ( $id );
		$data ['title'] = '查看每月下单金额 - 回扣管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'kickback/order' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回当月每天下单金额
	 */
	public function detail() {
		$id = $this->uri->segment(4);
		$date = $this->uri->segment(6);
		$this->load->model ( 'user_model' );
		$data ['user']=$this->user_model->load ( $id );
		$this->load->model ( 'order_model' );
		$data ['orders'] = $this->order_model->get_day_total_by_userid ( $id,$date);
		$data ['title'] = '查看当月每天下单金额 - 回扣管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'kickback/detail' );
		$this->load->view ( 'footer' );
	}
}